package cn.wolfcode.wolf.article.service.impl;


import cn.wolfcode.wolf.article.domain.StrategyCatalog;
import cn.wolfcode.wolf.article.mapper.StrategyCatalogMapper;
import cn.wolfcode.wolf.article.service.StrategyCatalogService;
import cn.wolfcode.wolf.article.utils.OssUtil;
import cn.wolfcode.wolf.article.vo.StrategyCatalogGroup;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Service
public class StrategyCatalogServiceImpl extends ServiceImpl<StrategyCatalogMapper, StrategyCatalog> implements StrategyCatalogService {


    @Override
    public List<StrategyCatalogGroup> findGroupList() {
        return getBaseMapper().selectGroupList();
    }

    @Override
    public JSONObject upload(MultipartFile file) {
        JSONObject result = new JSONObject();
        if(file==null){
            result.put("uploaded", 0);
            JSONObject error = new JSONObject();
            error.put("message", "请选择要上传的图片");
            result.put("error", error);
            return result;
        }
        // 调用阿里云 OSS 工具类进行文件上传
        // 解决文件名重复问题
        // 1. 直接使用 UUID 替换原始文件名
        // 2. 在原始文件名后面拼接时间戳
        String fileName =
            file.getOriginalFilename().substring(0, file.getOriginalFilename().lastIndexOf("."))+"_"+System.currentTimeMillis();
        // 返回阿里云可访问的 url 地址
        String url= OssUtil.upload("img", fileName, file);
        result.put("uploaded", 1);
        result.put("fileName", file.getOriginalFilename());
        result.put("url", url);
        return result;
    }



}
